package com.bsj.power.job.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsj.power.common.config.exception.ApiException;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.global.JsonResultEnum;
import com.bsj.power.common.def.dto.PageDTO;
import com.bsj.power.common.def.entity.exp.Course;
import com.bsj.power.common.def.entity.exp.TrainPlan;
import com.bsj.power.common.def.dto.job.CourseDTO;
import com.bsj.power.common.def.dto.job.TrainPlanDTO;
import com.bsj.power.common.mapper.CourseMapper;
import com.bsj.power.common.mapper.TrainPlanMapper;
import com.bsj.power.job.service.TrainPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author yinhao
 * @version 1.0
 * @description 培训计划
 * @date 2024/5/28
 */
@Slf4j
@Service
public class TrainPlanServiceImpl implements TrainPlanService {

    @Resource
    private TrainPlanMapper trainPlanMapper;

    @Resource
    private CourseMapper courseMapper;

    /**
     * 新增修改培训计划
     * @param vo
     * @return
     */
    @Override
    public JsonResult addAndUpdate(TrainPlanDTO vo) {
        Long trainId = vo.getTrainId();
        TrainPlan trainPlan = new TrainPlan();
        BeanUtil.copyProperties(vo, trainPlan);
        if (trainId == null) {
            //新增
            trainPlanMapper.insert(trainPlan);
        } else {
            //修改
            trainPlanMapper.updateById(trainPlan);
        }
        return JsonResult.success();
    }

    /**
     * 分页获取培训计划列表
     * @param dto
     * @return
     */
    @Override
    public JsonResult<List<TrainPlan>> pageList(PageDTO dto) {
        IPage<TrainPlan> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        QueryWrapper<TrainPlan> queryWrapper = new QueryWrapper<>();
        if (dto.getStartTime() != null && dto.getEndTime() != null) {
            queryWrapper.lambda().between(TrainPlan::getCreateTime, dto.getStartTime(), dto.getEndTime());
        }
        queryWrapper.lambda().orderByDesc(TrainPlan::getCreateTime);
        IPage<TrainPlan> iPage = trainPlanMapper.selectPage(page, queryWrapper);
        return JsonResult.success(iPage.getRecords(), iPage.getTotal());
    }

    /**
     * 根据ID获取培训计划
     * @param trainId
     * @return
     */
    @Override
    public JsonResult<TrainPlan> findIdByInfo(Long trainId) {
        TrainPlan trainPlan = trainPlanMapper.selectById(trainId);
        return JsonResult.success(trainPlan);
    }

    /**
     * 批量删除培训计划
     * @param trainIds
     * @return
     */
    @Override
    public JsonResult<Integer> deleteExp(Long[] trainIds) {
        int batchIds = trainPlanMapper.deleteBatchIds(Arrays.asList(trainIds));
        return JsonResult.success(batchIds);
    }

    /**
     * 新增或者修改培训课程
     * @param dto
     * @return
     */
    @Override
    public JsonResult addAndUpdateCourse(CourseDTO dto) {
        Long trainId = dto.getCourseId();
        Course course = new Course();
        BeanUtil.copyProperties(dto, course);
        if (trainId == null) {
            //新增
            courseMapper.insert(course);
        } else {
            //修改
            courseMapper.updateById(course);
        }
        return JsonResult.success();
    }

    /**
     * 删除培训课程
     * @param courseIds
     * @return
     */
    @Override
    public JsonResult<Integer> deleteCourse(Long[] courseIds) {
        List<Long> courseList = Arrays.asList(courseIds);
        List<Long> trainIds = trainPlanMapper.selectTrainByCourseIds(courseList);
        if (!trainIds.isEmpty()) {
            throw new ApiException(JsonResultEnum.BIND_TRAIN_IS_NOT_NULL);
        }
        return JsonResult.success(courseMapper.deleteBatchIds(courseList));
    }
}
